/**
 * @author fanxuean
 * @link fanshine124@gmail.com
 * @description 雇主任务列表页面包装器，采用惰性实例化单体模式，入口是init函数，返回包装器对象
 * @return Object {}
 */
MIDB.Model.MissionList = (function(){
    var uniqueInstance;
    function constructor() {
        var p = {};
        var curPage = 1;
        var pages = 1;
        var pageSize = 30;
        /**
         * [getMissionList 查询获取任务列表]
         * @param  {[Object]} sortName  [排序字段]
         * @param  {[Object]} sortOrder [排序类型]
         * @return {[Null]}             [无返回]
         */
        var getMissionList = function(sortName, sortOrder) {
            var param = {
                    userType: MIDB.cache.user.user_type,
                    userCode: MIDB.cache.user.user_code,
                    userName: MIDB.cache.user.user_name,
                    page: curPage,
                    pageSize: pageSize,
                    sortName: sortName,
                    sortOrder: sortOrder
                };

            $.ajax({
                url: MIDB.ajaxUrl.mission_list,
                type: 'POST',
                dataType: 'json',
                data: {atype: 'r', param: param, timestamp: new Date().getTime()},
                complete: function(xhr, textStatus) {
                  //called when complete
                },
                success: function(data, textStatus, xhr) {
                  if(!!data && data.Rows) {
                    compileMissionList(data.Rows);
                    compilePagination(data.curPage, data.pages);
                  }
                },
                error: function(xhr, textStatus, errorThrown) {
                  //called when there is an error
                }
            });

        };
        /**
         * [compileMissionList 渲染任务box]
         * @param  {[Array]} data [任务列表数据]
         * @return {[Null]}       [无返回]
         */
        var compileMissionList = function(data) {
            if(!data) return false;
            var tmpl = p.tmpl.html();
            var doTtmpl = doT.template(tmpl);
            var fragment = document.createDocumentFragment();
            $(data).each(function(event) {
                var divContainer = document.createElement('div');
                divContainer.innerHTML = doTtmpl(this);
                fragment.appendChild(divContainer);
                bindMissionBoxEvent(divContainer, this);
            });
            p.tmplContainer.html(fragment);
            // bindMissionEvent(p.tmplContainer);
        };
        /**
         * [compilePagination 渲染分页条]
         * @param  {[Integer]} cur [当前页]
         * @param  {[Integer]} all [总页数]
         * @return {[Null]}        [无返回]
         */
        var compilePagination = function(cur, all) {
            var pageContainer = $(p.contentContainer).find('.pagination'),
                tmpl = p.pageTmpl.html(),
                doTtmpl = doT.template(tmpl);
            curPage = cur;
            pages = all;
            pageContainer.html(doTtmpl({curPage: cur, pages: pages}));
            bindPageEvent(pageContainer);
        };
        /**
         * [bindPageEvent 绑定翻页事件]
         * @param  {[Object]} pageContainer [分页条对象]
         * @return {[Null]}               [无返回]
         */
        var bindPageEvent = function(pageContainer) {
            $(pageContainer)
            .find('.prePage').click(function(event) {
                if(curPage > 1) curPage--;
                getMissionList();
                event.stopPropagation();
            }).end()
            .find('.nextPage').click(function(event) {
                if(curPage < pages) curPage++;
                getMissionList();
                event.stopPropagation();
            }).end()
            .find('select[name=jumpPage]').change(function(event) {
                curPage = $(this).val();
                getMissionList();
                event.stopPropagation();
            });
        };
        /**
         * [bindMissionBoxEvent 绑定任务box事件]
         * @param  {[Object]} mlContainer [任务列表容器对象]
         * @return {[Null]}               [无返回]
         */
        var bindMissionBoxEvent = function(mlContainer, data) {
            var boxButtons = $(mlContainer).find('.mission_box_buttons');
            boxButtons.find('.modifyMissionBaseInfo').click(function(event) {
                MIDB.cache.frame.operateModel('missionAnnounce', {data: data, actype: 'modify'});
                event.stopPropagation();
            }).end()
            .find('.selectEmployee').click(function(event) {
                MIDB.cache.frame.operateModel('missionAnnounce', {data: data, actype: 'selectEmployee'});
                event.stopPropagation();
            }).end()
            .find('.employeesList').click(function(event) {

            }).end()
            .find('.appraiseMission').toggle(function(event) {
                $(mlContainer).find('.appraiseBox').removeClass('hidden');
            }, function() {
                $(mlContainer).find('.appraiseBox').addClass('hidden');
            }).end();
            $(mlContainer).find('.appraiseBox input[name=saveAppraise]').click(function(event) {
                var appraiseContent = $(this).siblings('.appraiseBoxText').val();
                saveAppraise({ac: appraiseContent, mid: data.m_code, mname: data.m_name});
            });
        };
        var saveAppraise = function(mp) {
            var param = {
              c_origin_code: MIDB.cache.user.user_code,
              c_origin_name: MIDB.cache.user.user_name,
              c_target_code: mp.mid,
              c_target_name: mp.mname,
              c_content: mp.ac
            };
            $.ajax({
              url: MIDB.ajaxUrl.comment_list,
              type: 'POST',
              dataType: 'text',
              data: {atype: 'c', param: param},
              complete: function(xhr, textStatus) {
                //called when complete
              },
              success: function(data, textStatus, xhr) {
                if(data == 1) {
                    alert('保存评价成功');
                }
              },
              error: function(xhr, textStatus, errorThrown) {
                alert('保存评价失败');
              }
            });

        };
        return {
            init: function(options) {
                p = $.extend({}, p||{}, options || {});
                curPage = 1;
                this.queryMissionList();

                return this;
            },
            queryMissionList: function(sortName, sortOrder) {

                getMissionList(sortName, sortOrder);
            }
        };
    }
    return {
        getInstance : function() {
            if (!uniqueInstance) {
                uniqueInstance = constructor();
            }
            return uniqueInstance;
        }
    };
})();